package com.xuexi;

import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * 代码：给一个正整数数组，将其中的元素排序，使之能组成一个最大的数，举例：nums=[10, 4, 62, 8]，结果862410
 */
public class Test {

    public static void main(String[] args) {
        int[] nums = new int[]{10, 4, 62, 8};
        System.out.println(num(nums));

        System.out.println(numToStr(nums));
    }


    public static String numToStr(int[] nums) {
        String stringNum = Arrays.stream(nums).mapToObj(String::valueOf).sorted(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        }).collect(Collectors.joining(""));
        return stringNum;
    }


    public static String num(int[] nums) {
        int[] is = new int[nums.length * 2];
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            while (num != 0) {
                int i1 = num % 10;
                is[j] = i1;
                num = num / 10;
                j++;
            }
        }
        StringBuilder sb = new StringBuilder();
        int[] newis = new int[j];
        System.arraycopy(is, 0, newis, 0, j);
        Arrays.sort(newis);
        for (int i = 0; i < j; i++) {
            sb.append(newis[i]);
        }
        StringBuilder reverse = sb.reverse();
        return reverse.toString();
    }
}
